package org.friping.mytool.views.custom.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.androidannotations.annotations.EBean;
import org.friping.mytool.R;

import java.util.Map;
import java.util.function.Consumer;

@EBean
public class DeleteAdapter extends AppAdapter {

    private boolean showDelete = false;
    private Consumer<Integer> deleteCallback = null;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = inflater.inflate(R.layout.it_delete, null);
        }

        Map itemData = (Map) this.getItem(position);
        TextView titleView = (TextView) convertView.findViewById(R.id.title);
        View nextView = convertView.findViewById(R.id.next);
        View deleteView = convertView.findViewById(R.id.delete);

        String title = (String) itemData.getOrDefault("title", "");
        if(title.length() == 0){
            title = (String) itemData.getOrDefault("name", "");
        }
        titleView.setText(title);

        deleteView.setOnClickListener((v) -> {
            if(deleteCallback != null){
                deleteCallback.accept(position);
            }
            removeItem(position);
        });

        if(showDelete){
            nextView.setVisibility(View.GONE);
            deleteView.setVisibility(View.VISIBLE);
        }else{
            deleteView.setVisibility(View.GONE);
            nextView.setVisibility(View.VISIBLE);
        }

        return convertView;
    }

    public boolean isShowDelete() {
        return showDelete;
    }

    public void setShowDelete(boolean showDelete) {
        this.showDelete = showDelete;
        this.notifyDataSetChanged();
    }

    public void setDeleteCallback(Consumer<Integer> deleteCallback) {
        this.deleteCallback = deleteCallback;
    }
}
